package com.evon.yardmanagement.ui.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;

import com.evon.yardmanagement.service.UtilityService;
import com.evon.yardmanagement.service.VendorManagementService;
import com.evon.yardmanagement.ui.dto.CountryDTO;
import com.evon.yardmanagement.ui.dto.VendorDTO;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Action class for filtering the data
 * 
 * @author vgaurav
 * 
 */
public class SuperAdminFilterAction extends ActionSupport implements
		ApplicationAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<CountryDTO> countryList = new ArrayList<CountryDTO>();
	private List<VendorDTO> vendorList = new ArrayList<VendorDTO>();

	private int selectedAdminCountryId;
	private int selectedAdminVendorId;

	private Map<String, Object> globalData;

	/**
	 * Initialie by populating the country list.
	 * 
	 * @return
	 */
	public String init() {
		populateValues();
		if (countryList == null || countryList.isEmpty()) {
			countryList = UtilityService.getCountries();
			globalData.put("COUNTRIES", countryList);
			vendorList = new ArrayList<VendorDTO>();
		}
		return SUCCESS;
	}

	/**
	 * Populate the list of vendors for the selected country.
	 * 
	 * @return
	 */
	public String getVendorsForCountry() {
		countryList = (List<CountryDTO>) globalData.get("COUNTRIES");
		vendorList = VendorManagementService
				.getVendorsForCountry(selectedAdminCountryId);
		globalData.put("VENDORS", vendorList);
		return SUCCESS;
	}

	/**
	 * Show all vendors for the selected country.
	 * 
	 * @return
	 */
	public String showVendors() {
		return SUCCESS;
	}

	public List<CountryDTO> getCountryList() {
		return countryList;
	}

	public List<VendorDTO> getVendorList() {
		return vendorList;
	}

	public void setCountryList(List<CountryDTO> countryList) {
		this.countryList = countryList;
	}

	public void setVendorList(List<VendorDTO> vendorList) {
		this.vendorList = vendorList;
	}

	public int getSelectedAdminCountryId() {
		return selectedAdminCountryId;
	}

	public int getSelectedAdminVendorId() {
		return selectedAdminVendorId;
	}

	public void setSelectedAdminCountryId(int selectedAdminCountryId) {
		this.selectedAdminCountryId = selectedAdminCountryId;
	}

	public void setSelectedAdminVendorId(int selectedAdminVendorId) {
		this.selectedAdminVendorId = selectedAdminVendorId;
	}

	/**
	 * Action method that will be implemented by each action class and provide
	 * implementation when vendor is selected.
	 * 
	 * @return
	 */
	public String onVendorSelect() {
		return SUCCESS;
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		globalData = application;
	}

	public void populateValues() {
		countryList = (List<CountryDTO>) globalData.get("COUNTRIES");
		vendorList = (List<VendorDTO>) globalData.get("VENDORS");
	}

}
